import allData from './mock'

import * as THREE from 'three'
import Experience from '../class/Experience';
import { EventEmitter } from 'events'
export default class Data extends EventEmitter {
    constructor() {
        super();

        console.log('======== 所有站点 ==========');
        console.log(allData);
        // 过滤出所有站点
        this.stations = this.createStations();

        // 过滤出所有线路
        this.lines = this.createLines();
    }

    //过滤出所有站点
    createStations() {
        const inStation = allData.map(item => {
            return {
                name: item.inStationName,
                level: item.inStationLevel,
                E: item.inStationLongitude,
                N: item.inStationLatitude,
            }
        })
        const outStation = allData.map(item => {
            return {
                name: item.outStationName,
                level: item.outStationLevel,
                E: item.outStationLongitude,
                N: item.outStationLatitude,
            }
        })
        const list = [...inStation, ...outStation];
        const map = new Map();
        list.forEach(item => {
            if (!map.has(item.name)) {
                map.set(item.name, item)
            }
        })
        // 第一次去重
        const arr = [...map.values()];

        // 第二次过滤
        const newArr = arr.filter(item => {
            return !item.name.includes("虚拟") && item.level !== '电厂' && !item.name.includes("退役")
        })
        newArr.forEach(item => item.color = this.stationColor(item.level))
        console.log('======== 去重后所有站点 ==========');
        console.log(newArr);
        return newArr
    }
    createLines() {


        const list = allData.filter(item => {
            const hasIn = item.inStationLongitude;
            const hasOut = item.outStationLongitude;
            const hasInLevel = item.inStationLevel !== '电厂'
            const hasOutLevel = item.outStationLevel !== '电厂'
            // const hasOutLevel = item.outStationLevel
            return hasIn && hasOut && hasOutLevel && hasInLevel
        })
        console.log("========== 线路第一次过滤 ==========");
        console.log(list);
        const lines = list.map(item => {
            return {
                start: [
                    item.inStationLongitude,
                    item.inStationLatitude
                ],
                end: [
                    item.outStationLongitude,
                    item.outStationLatitude
                ],
                includes: [
                    item.inStationName,
                    item.outStationName
                ],
                name: item.name,
                code: item.code,
                electricCarbonFactor: item.electricCarbonFactor,
                color: this.lineColor(item.electricCarbonFactor)
            }
        })
        console.log("========= 调整线路json ========");
        console.log(lines);
        return lines
    }

    stationColor(level = "") {
        if (!level) return '#ffffff'
        const colorList = Data.stationColorList();
        return colorList.find(item => item.name === level).color
        // return colorList[level]
    }
    static stationColorList() {
        return [
            { name: '1000kv', color: '#FF0000' },
            { name: '800kv', color: '#F86E23' },
            { name: '500kv', color: '#FFEC3D' },
            { name: '220kv', color: '#09DBFE' },
            { name: '110kv', color: '#00B22A' },
        ]
    }

    lineColor(electricCarbonFactor) {
        if (!electricCarbonFactor) return '#95FF7C'
        const float = parseFloat(electricCarbonFactor);
        const colorList = Data.lineColorRange()
        if (float > 0.8 && float <= 0.928) {
            return colorList[0].color
        } else if (float > 0.6 && float <= 0.8) {
            return colorList[1].color
        } else if (float > 0.4 && float <= 0.6) {
            return colorList[2].color
        } else if (float > 0.2 && float <= 0.4) {
            return colorList[3].color
        } else if (float > 0 && float <= 0.2) {
            return colorList[4].color
        } else {
            return '#000000'
        }
    }

    static lineColorRange() {
        return [
            { name: '(0.8000, 0.9280]', color: '#FA541C' },
            { name: '(0.6000, 0.8000]', color: '#FA8C16' },
            { name: '(0.4000, 0.6000]', color: '#FFEC3D' },
            { name: '(0.2000, 0.4000]', color: '#00FC3B' },
            { name: '(0, 0.2000]', color: '#95FF7C' },
        ]
    }
}